﻿
using Newtonsoft.Json;
using NurseStation.common;
using NurseStation.views;
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Prism.Events;
using Prism.Ioc;
using Prism.Modularity;
using Prism.Unity;
using Prism.Mvvm;
using Prism.Commands;
using Prism.Navigation.Regions;
using NurseStation.DataModels;
using NPOI.SS.Formula.Functions;
using Microsoft.VisualBasic.ApplicationServices;
using NurseStation.Services;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Data;
using System.Data;
using System.Windows.Controls;

namespace NurseStation.viewmodels
{
    public class DataForYearViewModel : VmBase
    {
        NurseDayWorkService nurseDayWorkService = new NurseDayWorkService();
        readonly Notifiaction notifiaction = Notifiaction.Instance;
        private DataForYear Win = null;

        public DateTime ChooseDATE = DateTime.Now;

        /// <summary>
        /// 点击保存
        /// </summary>
        public DelegateCommand<object> SaveCommand { get; private set; }

        private DataTable _DtRes;
        /// <summary>
        /// 数据源
        /// </summary>
        public DataTable DtRes
        {
            get
            {
                return _DtRes;
            }
            set
            {
                _DtRes = value;
                RaisePropertyChanged(nameof(DtRes));
            }
        }

        /// <summary>
        /// 构造
        /// </summary>
        public DataForYearViewModel(IContainerExtension container, IEventAggregator eventAggregator, IRegionManager regionManager) : base(container, eventAggregator, regionManager)
        {
            this.SaveCommand = new DelegateCommand<object>(OnSave);
            LoadData();
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        public void LoadData()
        {
            string year = ChooseDATE.ToString("yyyy");

            List<NurseDayWork> yearList = nurseDayWorkService.GetYearList(year);
            List<ColShow> listCol = nurseDayWorkService.GetDictCol();
            DataTable dt = new DataTable();
            //-------------------------数据列添加-----14列------
            dt.Columns.Add(new DataColumn() { ColumnName = "项目" });
            for (int i = 1; i <= 12; i++)
            {
                DataColumn column = new DataColumn();
                column.ColumnName = i.ToString() + "月";
                dt.Columns.Add(column);
            }
            dt.Columns.Add(new DataColumn() { ColumnName = "汇总" });
            //------------------------------------
            //行添加
            for (int i = 0; i < listCol.Count; i++)
            {
                DataRow dr = dt.NewRow();
                //第一个单元格
                dr[0] = listCol[i].Header;
                int rowSum = 0;
                for (int j = 1; j <= 12; j++)
                {
                    string mouth = new DateTime(int.Parse(year), j, 1).ToString("yyyy-MM");
                    var temp = yearList.FindAll(c => c.GROUPNAME == mouth).FirstOrDefault();
                    if (temp != null)
                        dr[j] = TriageEx.FindProValue<string>("C" + (i + 1).ToString(), temp);
                    else
                        dr[j] = "0";
                    rowSum += dr[j].ToSafeString().ToInt();
                }
                dr[13] = rowSum;
                //最后一个单元格
                dt.Rows.Add(dr);
            }
            DtRes = dt;
        }

        /// <summary>
        /// 设置窗体实例
        /// </summary>
        /// <param name="win"></param>
        public void SetWin(DataForYear win)
        {
            this.Win = win;
        }


        /// <summary>
        /// 点击保存
        /// </summary>
        public void OnSave(object o)
        {
            string year = ChooseDATE.ToString("yyyy");
            System.Windows.Forms.SaveFileDialog dialog1 = new System.Windows.Forms.SaveFileDialog();
            dialog1.FileName = string.Concat(new string[]
            {
                     Environment.GetFolderPath(Environment.SpecialFolder.Desktop),  "\\汇总查询数据"+year, ".xls"
            });
            dialog1.Filter = @"xls files(*.xls)|*.xls";
            if (dialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                ExcelHelper.Export(DtRes, string.Concat(new string[] { "老年护理工作量日报表统计( " + year + ")" }), dialog1.FileName);
            }
            notifiaction.AddNotifiaction(new NotifiactionModel() { Title = "提示", Content = "年度汇总数据已导出！", NotifiactionType = EnumPromptType.Success });

            this.Win.Close();
        }

    }
}
